package com.csoa.community.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.LinearLayout;

import com.collect.community.R;
import com.csoa.community.activity.DataStatisticsActivity;
import com.csoa.community.activity.FormActivity;
import com.csoa.community.activity.IssueGroupActivity;
import com.csoa.community.activity.LocalSpeciesTableActivity;
import com.csoa.community.activity.MainActivity;
import com.csoa.community.bean.IssueGroupBean;
import com.csoa.community.db.IssueGroupDB;

public class NarView extends LinearLayout {
	public static synchronized NarView getInst(Activity c, AttributeSet attr) {
		if (_inst == null) {
			_inst = new NarView(c, attr);
		}
		return _inst;
	}

	private static NarView _inst;
	private LinearLayout headView;

	private NarView(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public NarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public int[] ids = { R.id.nav_setting, R.id.nav_data_write, R.id.nav_query_statistics,
			R.id.nav_refer_info, R.id.nav_data_syncro, R.id.nav_system_help
			};
	private Context context;
	private Button nav_doc;
	private Button nav_email;
	private Button nav_chedule;
	private Button nav_notice;
	private Button nav_dynamic;
	private Button nav_daily;
	private Button nav_set;
	private Button nav_about;
	private Button nar_total;
	private View nav_layout;

	private void init() {
		headView = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.plug_navigation, null);
		nav_layout = headView.findViewById(R.id.nav_layout);
		nav_layout.setVisibility(View.INVISIBLE);
		nar_total = (Button) headView.findViewById(R.id.nar_total);
		// 导航按钮
		nar_total.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (nav_layout.getVisibility() == View.VISIBLE) {
					hideView(nar_total);
				} else {
					showView(nar_total);
				}
			}
		});

		nav_doc = (Button) headView.findViewById(ids[0]);
		nav_doc.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				set(ids[0]);
			}
		});

		nav_email = (Button) headView.findViewById(ids[1]);
		nav_email.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				set(ids[1]);
			}
		});

		nav_chedule = (Button) headView.findViewById(ids[2]);
		nav_chedule.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				set(ids[2]);
			}
		});

		nav_notice = (Button) headView.findViewById(ids[3]);
		nav_notice.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				set(ids[3]);
			}
		});

		nav_dynamic = (Button) headView.findViewById(ids[4]);
		nav_dynamic.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				set(ids[4]);
			}
		});

		nav_daily = (Button) headView.findViewById(ids[5]);
		nav_daily.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				set(ids[5]);
			}
		});


		headView.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		this.addView(headView);
	}

	public void showView(View v) {
		AnimationSet animSet = new AnimationSet(true);
		ScaleAnimation sa = new ScaleAnimation((float) v.getWidth()
				/ ((View) v.getParent()).getWidth(), 1.0f,
				(float) v.getHeight() / ((View) v.getParent()).getHeight(),
				1.0f, v.getLeft() + v.getWidth() / 2, v.getTop()
						+ v.getHeight() / 2);
		sa.setDuration(200);
		AlphaAnimation aa = new AlphaAnimation(0.2f, 1);
		aa.setDuration(200);
		animSet.addAnimation(sa);
		animSet.addAnimation(aa);
		nav_layout.setVisibility(View.VISIBLE);
		nav_layout.startAnimation(animSet);

	}
	
	public void hideView() {
		if (nav_layout.getVisibility() == View.VISIBLE) {
			hideView(nar_total);
		}
	}

	private void hideView(View v) {
		AnimationSet animSet = new AnimationSet(true);
		ScaleAnimation sa = new ScaleAnimation(1, (float) v.getWidth()
				/ ((View) v.getParent()).getWidth(), 1, (float) v.getHeight()
				/ ((View) v.getParent()).getHeight(), v.getLeft()
				+ v.getWidth() / 2, v.getTop() + v.getHeight() / 2);
		sa.setDuration(200);
		AlphaAnimation aa = new AlphaAnimation(1f, 0f);
		aa.setDuration(200);
		animSet.addAnimation(sa);
		animSet.addAnimation(aa);
		animSet.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				nav_layout.setVisibility(View.INVISIBLE);
			}
		});
		nav_layout.startAnimation(animSet);

	}

	public void set(int idx) {
		// nav_layout.setVisibility(View.GONE);
		hideView(nar_total);
		switch (idx) {
		case R.id.nav_setting:
			IssueGroupBean issue = IssueGroupDB.query();
			Intent intent = new Intent(context,
					IssueGroupActivity.class);
			intent.putExtra("IssueGroup", issue);
			context.startActivity(intent);
			break;
		case R.id.nav_data_write:
			startActivity(FormActivity.class);
			break;
		case R.id.nav_query_statistics:
			startActivity(DataStatisticsActivity.class);
			break;
		case R.id.nav_refer_info:
			startActivity(LocalSpeciesTableActivity.class);
			break;
		case R.id.nav_data_syncro:
//			startActivity(GongZuoActivity.class);
			break;
		case R.id.nav_system_help:
//			startActivity(RiChengActivity.class);
			break;
		}
	}

	/**
	 * 
	 */
	public void hiddenNav() {
		nav_layout.setVisibility(View.INVISIBLE);
	}

	public void startActivity(Class<?> activity) {
		Intent intent = new Intent(context, activity);
		context.startActivity(intent);
	}

	/**
	 * 进入主页的八大模块，初始化的时候先调用这个方法，用于导航栏标题显示此模块名
	 * 
	 * @param id模块的主页图片的R
	 *            id
	 */
	public void enterModule(int id) {
		Button button = (Button) findViewById(id);
		button.setVisibility(View.GONE);
	}
}
